package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lkd.dto.ReportDto;
import com.lkd.entity.Order;
import com.lkd.mapper.OrderMapper;
import com.lkd.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzy
 * @since 2025-11-01
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

//    @Lazy
//    @Autowired
//    private IOrderService orderService;

    /**
     * 销售额统计
     *
     * @param reportDto
     * @return
     */
    @Override
    public Integer getOrderAmountByTime(ReportDto reportDto) {


        LambdaQueryWrapper<Order> eq = new LambdaQueryWrapper<Order>()
                .lt(Order::getCreateTime, reportDto.getEnd())
                .gt(Order::getCreateTime, reportDto.getStart())
                .eq(Order::getStatus, 1);
        List<Order> list = this.list(eq);
        return list.stream().mapToInt(Order::getAmount).sum();

//        this.lambdaQuery()
//                .lt(Order::getCreateTime, reportDto.getEnd())
//                .gt(Order::getCreateTime, reportDto.getStart())
//                .eq(Order::getStatus, 1);

    }

    /**
     * 订单统计
     *
     * @param reportDto
     */
    @Override
    public Integer searchByTime(ReportDto reportDto) {

//        LambdaQueryWrapper<Order> gt = new LambdaQueryWrapper<Order>()
//                .lt(Order::getCreateTime, reportDto.getEnd())
//                .gt(Order::getCreateTime, reportDto.getStart());
//
//        List<Order> list = this.list(gt);
        Long count = this.lambdaQuery()
                .lt(Order::getCreateTime, reportDto.getEnd())
                .gt(Order::getCreateTime, reportDto.getStart())
                .count();

        return Math.toIntExact(count);
    }
}
